﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Text;
using System.Linq;
using System.Xml.Linq;

namespace RecToTPI.Utility
{
    public static class Tools
    {
        private static readonly object mark = new object();
        private static Dictionary<int, string> _tfoFormatDictionary;
        /// <summary>
        /// TFO字段类型/字段名缓存字典
        /// </summary>
        public static Dictionary<int, string> TFOFormatDict
        {
            get
            {
                if (_tfoFormatDictionary == null)
                {
                    lock (mark)
                    {
                        string xDocPath = AppDomain.CurrentDomain.BaseDirectory + @"\TFOFormat.xml";
                        XDocument xDoc = XDocument.Load(xDocPath);
                        var xElements = xDoc.Root.Descendants("FieldMap");
                        if (xElements.Any())
                        {
                            _tfoFormatDictionary = new Dictionary<int, string>();
                            foreach (var item in xElements)
                            {
                                _tfoFormatDictionary.Add(int.Parse(item.Element("TypeNumber").Value), item.Element("FieldNameCN").Value);
                            }
                            return _tfoFormatDictionary;
                        }
                    }
                }
                return _tfoFormatDictionary;
            }
        }
    }
}
